{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "7db681c6-dd28-4c3f-b345-62d920a4e5ec"
   },
   "source": [
    "## MNIST-DNN 简单DNN完成手写数字识别\n",
    "\n",
    "这个简单用例用于展示如何利用Alibaba的PAI-DSW（Data Science Workspace）编写一个简单DNN的模型来识别手写数字，也就是深度学习中的HelloWorld项目。\n",
    "\n",
    "首先我们下载MNIST需要的数据，这个数据包括大量28x28图片，我们把数据分为2个数据集，55000个训练数据、10000个测试数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "uuid": "3e8c89a1-a43e-4b99-9f89-d114fe65625e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data/train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data/t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data/t10k-labels-idx1-ubyte.gz\n",
      "((55000, 784), (55000, 10))\n",
      "((10000, 784), (10000, 10))\n"
     ]
    }
   ],
   "source": [
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "mnist = input_data.read_data_sets(\"MNIST_data/\", one_hot=True)\n",
    "\n",
    "print(mnist.train.images.shape, mnist.train.labels.shape)\n",
    "print(mnist.test.images.shape, mnist.test.labels.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "12d7a111-baee-4c3a-a549-304f5a5df16b"
   },
   "source": [
    "我们构建一个简单1层的DNN，同时我们把该Tensorflow的Graph输出给TensorBoard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "uuid": "70b41cb3-0bf1-45a1-a428-8b61859c6a42"
   },
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "sess = tf.InteractiveSession()\n",
    "writer = tf.summary.FileWriter('./summary/mnist', sess.graph)\n",
    "x = tf.placeholder(tf.float32, [None, 784])\n",
    "\n",
    "W = tf.Variable(tf.zeros([784, 10]))\n",
    "b = tf.Variable(tf.zeros([10]))\n",
    "\n",
    "y = tf.nn.softmax(tf.matmul(x, W) + b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "05bbf6c7-2dbb-4d1a-b1e9-8773207e2025"
   },
   "source": [
    "我们定义一个损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "uuid": "51365c47-b00b-4889-aae6-77520931482c"
   },
   "outputs": [],
   "source": [
    "y_ = tf.placeholder(tf.float32, [None, 10])\n",
    "cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "1357980f-576e-4d2a-97e8-945e9ebd5e70"
   },
   "source": [
    "我们选择一个训练器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "uuid": "399626d5-cdee-4544-9aad-c4f1c24cd250"
   },
   "outputs": [],
   "source": [
    "train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "6add7906-42c5-4f4d-97ff-c95e59eceec3"
   },
   "source": [
    "开始训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "uuid": "50f28398-7d03-4658-9bcb-95fecc2dd9bd"
   },
   "outputs": [],
   "source": [
    "\n",
    "tf.global_variables_initializer().run()\n",
    "\n",
    "for i in range(1000):\n",
    "    batch_xs, batch_ys = mnist.train.next_batch(100)\n",
    "    train_step.run({x: batch_xs, y_: batch_ys})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "cellType": "markdown",
    "uuid": "6f58946a-f81f-417f-aefe-b260f44bdf6d"
   },
   "source": [
    "输出验证结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "uuid": "2c7d90c4-794d-42d2-a270-08b8042b954f"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.9179\n"
     ]
    }
   ],
   "source": [
    "correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))\n",
    "\n",
    "accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))\n",
    "\n",
    "print(accuracy.eval({x: mnist.test.images, y_: mnist.test.labels}))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "输出TensorBoard需要的文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "uuid": "1898578f-898a-40ff-8640-2a18402b8755"
   },
   "outputs": [],
   "source": [
    "writer.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "然后我们在JupyterLab添加Tensorboard Tab，就可以浏览在workspace下所有的tensorboard的实例了, 如果你在workspace下有多个TensorBoard的log目录，你可以在Run的下拉框中选择你需要\n",
    "![tensorboard](./tensorboard.jpg)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
